package com.retail.proxy.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.retail.core.entity.ParamReq;
import com.retail.proxy.entity.request.ProxyMechanismCategoryRequest;
import com.retail.proxy.entity.request.ProxyOrderAchieveRequest;
import com.retail.proxy.entity.vo.ProxyMechanismCategoryVo;
import com.retail.proxy.entity.vo.ProxyOrderAchieveVo;
import com.retail.proxy.mapper.ProxyMechanismCategoryMapper;
import com.retail.proxy.mapper.ProxyOrderAchieveMapper;
import com.retail.proxy.service.ProxyMechanismCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代理机制 Service impl
 *
 * @author Administrator
 */
@Service
public class ProxyMechanismCategoryServiceImpl implements ProxyMechanismCategoryService {

    @Resource
    private ProxyMechanismCategoryMapper proxyMechanismCategoryMapper;

    @Resource
    private ProxyOrderAchieveMapper proxyOrderAchieveMapper;

    @Override
    public ProxyMechanismCategoryVo getModel(Long id) {
        List<ProxyOrderAchieveVo> allProxyOrderAchieve = proxyOrderAchieveMapper.findAllOrderAchieves();
        Map<String, ProxyOrderAchieveVo> initData = pojoToMap(allProxyOrderAchieve);

        ProxyMechanismCategoryVo vo = proxyMechanismCategoryMapper.getModel(id);
        ProxyOrderAchieveVo yearAchieve = initData.get(vo.getId() + "_" + 0);
        if (ObjectUtil.isEmpty(yearAchieve)) {
            yearAchieve = new ProxyOrderAchieveVo();
        }
        yearAchieve.setProxyCategoryId(id);
        yearAchieve.setAchieveType(0);
        List<ProxyOrderAchieveVo> retyearAchieve = new ArrayList<>();
        retyearAchieve.add(yearAchieve);
        retyearAchieve.add(yearAchieve);
        vo.setYearAchieve(retyearAchieve);

        List<ProxyOrderAchieveVo> monthAchieve = new ArrayList<>();
        for (Integer i = 1; i <= 12; i++) {
            ProxyOrderAchieveVo monthVo = initData.get(vo.getId() + "_" + 1 + "_" + i);
            if (ObjectUtil.isEmpty(monthVo)) {
                monthVo = new ProxyOrderAchieveVo();
            }
            monthVo.setAchieveType(1);
            monthVo.setAchieveMonth(i);
            monthVo.setProxyCategoryId(vo.getId());
            monthAchieve.add(monthVo);
        }
        vo.setMonthAchieve(monthAchieve);
        return vo;
    }

    @Transactional
    @Override
    public Long save(ProxyMechanismCategoryRequest request) {
        proxyMechanismCategoryMapper.save(request);
        return request.getId();
    }

    @Override
    public Long update(ProxyMechanismCategoryRequest request) {
        proxyMechanismCategoryMapper.update(request);
        List<ProxyOrderAchieveRequest> yearAcheve = request.getYearAchieve();
        for (Integer i = 0; i < yearAcheve.size(); i++) {
            ProxyOrderAchieveRequest proxyOrderAchieveRequest = yearAcheve.get(i);
            ProxyOrderAchieveVo yearAch = proxyOrderAchieveMapper.findAchieveByCategoryIdAndAchieveType(proxyOrderAchieveRequest.getProxyCategoryId(), proxyOrderAchieveRequest.getAchieveType());
            if (ObjectUtil.isEmpty(yearAch)) {
                proxyOrderAchieveMapper.save(proxyOrderAchieveRequest);
            } else {
                proxyOrderAchieveRequest.setId(yearAch.getId());
                if (i % 2 == 0) {
                    proxyOrderAchieveMapper.updateAcheive1Data(proxyOrderAchieveRequest);
                }

                if (i % 2 != 0) {
                    proxyOrderAchieveMapper.updateAcheive2Data(proxyOrderAchieveRequest);
                }
            }
        }

        List<ProxyOrderAchieveRequest> monthAcheve = request.getMonthAchieve();
        for (Integer i = 0; i < monthAcheve.size(); i++) {
            ProxyOrderAchieveRequest ache = monthAcheve.get(i);
            ProxyOrderAchieveVo yearAch = proxyOrderAchieveMapper.findAchieveByCategoryIdAndAchieveTypeAndMonth(ache.getProxyCategoryId(), ache.getAchieveType(), ache.getAchieveMonth());
            if (ObjectUtil.isEmpty(yearAch)) {
                proxyOrderAchieveMapper.save(ache);
            } else {
                ache.setId(yearAch.getId());
                if (i % 2 == 0) {
                    proxyOrderAchieveMapper.updateAcheive1Data(ache);
                }

                if (i % 2 != 0) {
                    proxyOrderAchieveMapper.updateAcheive2Data(ache);
                }
            }
        }

        return request.getId();
    }

    @Override
    public List<ProxyMechanismCategoryVo> listPage(ParamReq<ProxyMechanismCategoryRequest> params) {
        params.setStartIndex((params.getCurrent() - 1) * params.getSize());
        List<ProxyMechanismCategoryVo> list = proxyMechanismCategoryMapper.listPage(params);
        List<ProxyOrderAchieveVo> allProxyOrderAchieve = proxyOrderAchieveMapper.findAllOrderAchieves();
        Map<String, ProxyOrderAchieveVo> initData = pojoToMap(allProxyOrderAchieve);

        list.forEach(vo -> {
//            ProxyOrderAchieveVo yearAchieve = proxyOrderAchieveMapper.findAchieveByCategoryIdAndAchieveType(vo.getId(), 0);
            ProxyOrderAchieveVo yearAchieve = initData.get(vo.getId() + "_" + 0);
            if (ObjectUtil.isEmpty(yearAchieve)) {
                yearAchieve = new ProxyOrderAchieveVo();
            }
            yearAchieve.setProxyCategoryId(vo.getId());
            yearAchieve.setAchieveType(0);
            List<ProxyOrderAchieveVo> retyearAchieve = new ArrayList<>();
            retyearAchieve.add(yearAchieve);
            retyearAchieve.add(yearAchieve);
            vo.setYearAchieve(retyearAchieve);

            List<ProxyOrderAchieveVo> monthAchieve = new ArrayList<>();
            for (Integer i = 1; i <= 12; i++) {
//                ProxyOrderAchieveVo monthVo = proxyOrderAchieveMapper.findAchieveByCategoryIdAndAchieveTypeAndMonth(vo.getId(), 1, i);
                ProxyOrderAchieveVo monthVo = initData.get(vo.getId() + "+" + 1 + "_" + i);
                if (ObjectUtil.isEmpty(monthVo)) {
                    monthVo = new ProxyOrderAchieveVo();
                }
                monthVo.setAchieveType(1);
                monthVo.setAchieveMonth(i);
                monthVo.setProxyCategoryId(vo.getId());
                monthAchieve.add(monthVo);
                monthAchieve.add(monthVo);
            }
            vo.setMonthAchieve(monthAchieve);
        });

        return list;
    }

    private Map<String, ProxyOrderAchieveVo> pojoToMap(List<ProxyOrderAchieveVo> achieves) {
        Map<String, ProxyOrderAchieveVo> initMap = new HashMap<>();
        achieves.forEach(achieve -> {
            Integer achieveType = achieve.getAchieveType();
            Long proxyCategoryId = achieve.getProxyCategoryId();
            Integer achieveMonth = achieve.getAchieveMonth();
            if (achieveType == 0) {
                initMap.put(proxyCategoryId + "_" + achieveType, achieve);
            } else if (achieveType == 1) {
                initMap.put(proxyCategoryId + "_" + achieveType + "_" + achieveMonth, achieve);
            }
        });

        return initMap;

    }


    @Override
    public Integer countPage(ParamReq<ProxyMechanismCategoryRequest> params) {
        return proxyMechanismCategoryMapper.countPage(params);
    }

    @Override
    public Integer updateDelFlagById(Long id, Integer delFlag) {
        return proxyMechanismCategoryMapper.updateDelFlagById(id, delFlag);
    }

    @Override
    public List<ProxyMechanismCategoryVo> findMechanismCategoryQtCurrentLevel(Long adminId) {
        return proxyMechanismCategoryMapper.findMechanismCategoryQtCurrentLevel(adminId);
    }
}
